//
//  TranslationManager.m
//  ImGuider X
//
//  Created by 陆涛 on 2018/10/15.
//  Copyright © 2018 imguider. All rights reserved.
//

#import "TranslationManager.h"
#import "Const_OC.h"

#define kTranslationArray   @"kTranslationArray"
#define kLeftLanguage       @"kLeftLanguage"
#define kRightLanguage      @"kRightLanguage"
#define kRecentLanguage     @"kRecentLanguage"


@implementation TranslationManager

+ (instancetype)sharedManager {
    
    static TranslationManager *_sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _sharedManager = [[TranslationManager alloc] init];
        
    });
    return _sharedManager;
}

- (void)setTranslationArray:(NSMutableArray *)translationArray {
    
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:translationArray];
    
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:kTranslationArray];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSMutableArray *)translationArray {
    
    NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:kTranslationArray];
    
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}


- (void)setLanguageLeft:(LanguageModel *)languageLeft {
    
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:languageLeft];
    
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:kLeftLanguage];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (LanguageModel *)languageLeft {
    
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kLeftLanguage];
    LanguageModel * model = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    if (!model) {
        model = [LanguageModel new];
        model.lang = @"zh-CN";
        model.translation =@"zh";
        model.name_local = @"trans_1";
    }
    return model;
}

- (void)setLanguageRight:(LanguageModel *)languageRight {
    
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:languageRight];
    
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:kRightLanguage];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (LanguageModel *)languageRight{
    
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kRightLanguage];
    LanguageModel * model = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    if (!model) {
        model = [LanguageModel new];
        model.lang = @"en-AU";
        model.translation =@"en";
        model.name_local = @"trans_3";
    }
    return model;
}

- (void)setRecentLanguages:(NSArray *)recentLanguages {
    
     NSData *data = [NSKeyedArchiver archivedDataWithRootObject:recentLanguages];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:kRecentLanguage];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSArray *)recentLanguages {

    NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:kRecentLanguage];
    
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

@end

